package actions;

import models.UserModel;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("json-default")
public class LogInAction extends CommonAction implements ModelDriven<UserModel> {
	private static final long serialVersionUID = 1L;
	private static final Logger LOGGER = Logger.getLogger(LogInAction.class);
	private UserModel model;

	@Autowired
	public void setModel(UserModel model) {
		this.model = model;
	}

	public UserModel getModel() {
		return model;
	}

	@Action(value = "getUserLogedIn", results = { @Result(name = "success", type = "json") })
	public String execute() {
		LOGGER.info("LogInAction - Start main action.");
		Authentication auth = SecurityContextHolder.getContext()
				.getAuthentication();
		model.setName(auth.getName());
		LOGGER.info("LogInAction - End main action.");
		return SUCCESS;
	}
}
